package server;



import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromiseNotifier;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.extern.slf4j.Slf4j;
import rpc.NamedThreadFactory;

import java.util.concurrent.ExecutorService;

@Slf4j
public class Netty4Server implements Server{
    private final int PROCESSORS = Runtime.getRuntime().availableProcessors();
    private final ServerBootstrap serverBootstrap;

    public Netty4Server() {
        NamedThreadFactory bossThread = new NamedThreadFactory("boss");
        NamedThreadFactory workThread = new NamedThreadFactory("work");
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(PROCESSORS, bossThread);
        NioEventLoopGroup workGroup = new NioEventLoopGroup(PROCESSORS * 2, workThread);
//        workGroup.setIoRatio();
        serverBootstrap = new ServerBootstrap()
                .group(bossGroup, workGroup)
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.TCP_NODELAY, true);
    }

    @Override
    public void start(int port, ExecutorService threadPool) {
        serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast();
            }
        });





//        log.info();
//        log.error("asdf");
//        log.info("The test info is :{}", testInfo);
    }

    @Override
    public void registerProcess() {

    }

    @Override
    public void stop() throws Exception {

    }
}
